home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Expanders / BOOPSI / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  5.0 KB  |  199 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #define BOOPSI_IEX
  15.  
  16. #include "DEV_IE:Expanders/defs.h"
  17.  
  18. extern __geta4 struct Library *LibInit   ( __A0 BPTR );
  19. extern __geta4 struct Library *LibOpen   ( __D0 long, __A0 struct Library * );
  20. extern __geta4 long            LibClose  ( __A0 struct Library * );
  21. extern __geta4 long            LibExpunge( __A0 struct Library * );
  22.  
  23. struct BOOPSIExp *LibBase = NULL;
  24.  
  25. long SysBase        = NULL;
  26. long DOSBase        = NULL;
  27. long IntuitionBase  = NULL;
  28. long GfxBase        = NULL;
  29. long GadToolsBase   = NULL;
  30. BPTR SegList        = 0;
  31. UBYTE *Desc         = NULL;
  32.  
  33. /*
  34.  *    The Initialization routine is given only a seglist pointer.  Since
  35.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  36.  *    and return either NULL or the library pointer.  Exec has Forbid()
  37.  *    for us during the call.
  38.  */
  39.  
  40. struct Library *LibInit( __A0 BPTR segment )
  41. {
  42.  
  43.     struct Library *lib = NULL;
  44.  
  45.     static const long Vectors[] = {
  46.  
  47.     (long)ALibOpen,         /*  standard lib functions    */
  48.     (long)ALibClose,
  49.     (long)ALibExpunge,
  50.     (long)ALibReserved,
  51.  
  52.     (long)IEX_Mount,        /*  mount function            */
  53.  
  54.     (long)IEX_Add,          /*  edit functions            */
  55.     (long)IEX_Remove,
  56.     (long)IEX_Edit,
  57.     (long)IEX_Copy,
  58.     (long)IEX_Make,
  59.     (long)IEX_Free,
  60.     (long)IEX_Refresh,
  61.  
  62.     (long)IEX_Save,         /*  I/O functions             */
  63.     (long)IEX_Load,
  64.  
  65.     (long)IEX_StartSrcGen,  /*  source related functions  */
  66.     (long)IEX_WriteGlobals, 
  67.     (long)IEX_WriteSetup,
  68.     (long)IEX_WriteCloseDown,
  69.     (long)IEX_WriteHeaders,
  70.     (long)IEX_WriteRender,
  71.     (long)IEX_GetIDCMP,
  72.     (long)IEX_WriteData,
  73.     (long)IEX_WriteChipData,
  74.     (long)IEX_WriteOpenWnd,
  75.     (long)IEX_WriteCloseWnd,
  76.     -1
  77.     };
  78.  
  79.     SysBase = *(long *)4;
  80.  
  81.     if( DOSBase = OpenLibrary( "dos.library", 36 )) {
  82.     if( IntuitionBase = OpenLibrary( "intuition.library", 36 )) {
  83.         if( GfxBase = OpenLibrary( "graphics.library", 36 )) {
  84.         if( GadToolsBase = OpenLibrary( "gadtools.library", 36 )) {
  85.  
  86.             if( LibBase = lib = MakeLibrary( (APTR)Vectors, NULL, NULL, sizeof( struct BOOPSIExp ), NULL )) {
  87.  
  88.             lib->lib_Node.ln_Type = NT_LIBRARY;
  89.             lib->lib_Node.ln_Name = LibName;
  90.             lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  91.             lib->lib_Version      = 37;
  92.             lib->lib_Revision     = 0;
  93.             lib->lib_IdString     = (APTR)LibId;
  94.  
  95.             SegList = segment;
  96.  
  97.             AddLibrary( lib );
  98.             }
  99.  
  100.         }
  101.         }
  102.     }
  103.     }
  104.  
  105.     return( lib );
  106. }
  107.  
  108. /*
  109.  *    Open is given the library pointer and the version request.  Either
  110.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  111.  *    Exec has Forbid() for us during the call.
  112.  */
  113.  
  114. struct Library *LibOpen( __D0 long version, __A0 struct Library *lib )
  115. {
  116.     ++lib->lib_OpenCnt;
  117.  
  118.     lib->lib_Flags &= ~LIBF_DELEXP;
  119.  
  120.     return( lib );
  121. }
  122.  
  123. /*
  124.  *    Close is given the library pointer and the version request.  Be sure
  125.  *    not to decrement the open count if already zero.  If the open count
  126.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  127.  *    and return the seglist.  Otherwise we return NULL.
  128.  *
  129.  *    Note that this routine never sets LIBF_DELEXP on its own.
  130.  *
  131.  *    Exec has Forbid() for us during the call.
  132.  */
  133.  
  134. long LibClose( __A0 struct Library *lib )
  135. {
  136.     if( lib->lib_OpenCnt && --lib->lib_OpenCnt )
  137.     return( NULL );
  138.  
  139.     if( lib->lib_Flags & LIBF_DELEXP )
  140.     return( LibExpunge( lib ));
  141.  
  142.     return( NULL );
  143. }
  144.  
  145. /*
  146.  *    We expunge the library and return the Seglist ONLY if the open count
  147.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  148.  *    flag and return NULL.
  149.  *
  150.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  151.  *    might be called from the memory allocator and thus we CANNOT DO A
  152.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  153.  *
  154.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  155.  *    therefore freeze if we called it ourselves.  As far as I can tell
  156.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  157.  *    below.
  158.  */
  159.  
  160. long LibExpunge( __A0 struct Library *lib )
  161. {
  162.     if( lib->lib_OpenCnt ) {
  163.  
  164.     lib->lib_Flags |= LIBF_DELEXP;
  165.     return( NULL );
  166.     }
  167.  
  168.     Remove( &lib->lib_Node );
  169.  
  170.     FreeMem(( char * )lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize );
  171.  
  172.     if( DOSBase ) {
  173.     CloseLibrary( (struct Library *)DOSBase );
  174.     DOSBase = NULL;
  175.     }
  176.  
  177.     if( IntuitionBase ) {
  178.     CloseLibrary( (struct Library *)IntuitionBase );
  179.     IntuitionBase = NULL;
  180.     }
  181.  
  182.     if( GfxBase ) {
  183.     CloseLibrary( (struct Library *)GfxBase );
  184.     GfxBase = NULL;
  185.     }
  186.  
  187.     if( GadToolsBase ) {
  188.     CloseLibrary( (struct Library *)GadToolsBase );
  189.     GadToolsBase = NULL;
  190.     }
  191.  
  192.     if( Desc ) {          /* free the descriptions file  */
  193.     FreeVec( Desc );  /* loaded by IEX_Mount         */
  194.     Desc = NULL;
  195.     }
  196.  
  197.     return(( long )SegList );
  198. }
  199.